package com.rybbaby.tss.core.hqlsearch.parse.impl;

import com.rybbaby.tss.core.hqlsearch.parse.IHqlParse;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 * @description 字符串类型条件解析
 * 
 * @author 谢进伟
 * 
 * @createDate 2015-11-11 上午9:55:13
 */
public class StringParseImpl implements IHqlParse {
	
	private static final String SUFFIX_COMMA = ",";
	private static final String SUFFIX_KG = " ";
	/** 模糊查询符号 */
	private static final String SUFFIX_ASTERISK = "*";
	private static final String SUFFIX_ASTERISK_VAGUE = "%";
	/** 不等于查询符号 */
	private static final String SUFFIX_NOT_EQUAL = "!";
	private static final String SUFFIX_NOT_EQUAL_NULL = "!NULL";
	
	public void addCriteria(DetachedCriteria cq , String name , Object value) {
		String searchValue = null;
		if(value != null && (searchValue = value.toString().trim()) != "") {
			// [1].In 多个条件查询{逗号隔开参数}
			if(searchValue.indexOf(SUFFIX_COMMA) >= 0) {
				// 页面输入查询条件，情况（取消字段的默认条件）
				if(searchValue.indexOf(SUFFIX_KG) >= 0) {
					String val = searchValue.substring(searchValue.indexOf(SUFFIX_KG));
					cq.add(Restrictions.eq(name , val));
				} else {
					Object [] vs = searchValue.split(SUFFIX_COMMA);
					cq.add(Restrictions.in(name , vs));
				}
			}
			// [2].模糊查询{带有* 星号的参数}
			else if(searchValue.indexOf(SUFFIX_ASTERISK) >= 0) {
				cq.add(Restrictions.like(name , searchValue.replace(SUFFIX_ASTERISK , SUFFIX_ASTERISK_VAGUE)));
			}
			// [3].不匹配查询{等于！叹号}
			// (1).不为空字符串
			else if(searchValue.equals(SUFFIX_NOT_EQUAL)) {
				cq.add(Restrictions.isNotNull(name));
			}
			// (2).不为NULL
			else if(searchValue.toUpperCase().equals(SUFFIX_NOT_EQUAL_NULL)) {
				cq.add(Restrictions.isNotNull(name));
			}
			// (3).正常不匹配
			else if(searchValue.indexOf(SUFFIX_NOT_EQUAL) >= 0) {
				cq.add(Restrictions.ne(name , searchValue.replace(SUFFIX_NOT_EQUAL , "")));
			}
			// [4].全匹配查询{没有特殊符号的参数}
			else {
				cq.add(Restrictions.like(name , SUFFIX_ASTERISK_VAGUE + searchValue + SUFFIX_ASTERISK_VAGUE));
			}
		}
		
	}
	
	public void addCriteria(DetachedCriteria cq , String name , Object value , String beginValue , String endValue) {
		addCriteria(cq , name , value);
	}
	
}
